//  -----------------------------------------------------------------------------------------------//
//   This file is part of SvnExternalUpdater                                                               //
//   Copyright (C)2008 Romain Verdier ( romain@codingly.com )                                      //
//                                                                                                 //
//   This program is free software; you can redistribute it and/or modify it under the terms       //
//   of the GNU General Public License as published by the Free Software Foundation; either        //
//   version 2 of the License, or (at your option) any later version. This program is distributed  //
//   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied        //
//   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  //
//   License for more details.                                                                     //
//                                                                                                 //
//   You should have received a copy of the GNU General Public License along with this program; if //
//   not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. or  //
//   see http://www.fsf.org/licensing/licenses/gpl.html                                            //
//  -----------------------------------------------------------------------------------------------//
using System.ComponentModel;
using System.Diagnostics;
using Codingly.SvnExternalUpdater.Core.Model;
using Codingly.SvnExternalUpdater.Core.Services;

namespace Codingly.SvnExternalUpdater.Core
{
	public class TortoiseService : ITortoiseService
	{
		public void Update(string path)
		{
			string tortoisePath; // = ConfigurationManager.AppSettings["tortoiseProcPath"];
			//if (string.IsNullOrEmpty(tortoisePath))
			{
				tortoisePath = "tortoiseproc.exe";
			}

			string args = string.Format("/command:update /path:\"{0}\"", path);

			try
			{
				Process process = Process.Start(tortoisePath, args);
				if (process != null)
				{
					process.WaitForExit();
				}
			}
			catch (Win32Exception)
			{
				// todo
				throw;
			}
		}

		#region ITortoiseService Members

		public void ShowLog(IExternal external)
		{
			string tortoisePath; //= ConfigurationManager.AppSettings["tortoiseProcPath"];
			//  if (string.IsNullOrEmpty(tortoisePath))
			{
				tortoisePath = "tortoiseproc.exe";
			}

			string args = string.Format("/command:log /path:\"{0}\"", external.Url);
			if (external.HasRevision)
			{
				args += string.Format(" /startrev:{0} /endrev:{1}", external.LocalRevision, external.RemoteRevision);
			}

			try
			{
				Process.Start(tortoisePath, args);
			}
			catch (Win32Exception)
			{
				// todo
				throw;
			}
		}

		public void OpenPath(string path)
		{
			Process.Start(path);
		}

		#endregion
	}
}